package cn.itcast.service.impl;

import cn.itcast.constant.RedisConstant;
import cn.itcast.dao.CheckGroupDao;
import cn.itcast.dao.SetmealDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.CheckGroup;
import cn.itcast.pojo.Setmeal;
import cn.itcast.service.CheckGroupService;
import cn.itcast.service.SetmealService;
import cn.itcast.utils.QiniuUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;

    /**
     * 收藏的增添方法
     * @param setmeal
     * @param checkGroupIds
     */
    @Override
    public void add(Setmeal setmeal, Integer[] checkGroupIds) {
        setmealDao.add(setmeal);
        Integer setmealId = setmeal.getId();
        this.setSetmealAndCheckGroup(setmealId, checkGroupIds);
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
    }

    /**
     * 分页查询套餐数据
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = setmealDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 删除套餐数据及关联关系
     * @param id
     * @return
     */
    @Override
    public void delete(int id) {
        Setmeal setmeal = setmealDao.findById(id);
        //QiniuUtils.deleteFileFromQiniu(setmeal.getImg());
        //删除在redis里setmealPicDbResources列的数据，之后由redis定时删除七牛云的数据
        jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
        setmealDao.delSetmealAndCheckGroup(id);
        setmealDao.delete(id);
    }

    /**
     * 查询套餐和检查组的关联关系
     * @param id
     * @return
     */
    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(int id) {
        return setmealDao.findCheckGroupIdsBySetmealId(id);
    }

    /**
     * 修改套餐数据
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
        Integer setmealId = setmeal.getId();
        Setmeal setmealold = setmealDao.findById(setmealId);
        String oldImg = setmealold.getImg();
        String img = setmeal.getImg();
        //判断是否修改过图片
        if (!oldImg.equalsIgnoreCase(img)){
            //QiniuUtils.deleteFileFromQiniu(oldImg);
            //删除在redis里setmealPicDbResources列的数据，之后由redis定时删除七牛云的数据
            jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,oldImg);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,img);
        }
        //删除关联关系
        setmealDao.delSetmealAndCheckGroup(setmealId);
        //重新建立关联关系
        this.setSetmealAndCheckGroup(setmealId,checkgroupIds);
        //修改套餐数据
        setmealDao.edit(setmeal);
    }

    /**
     * 查询所有套餐数据
     * @return
     */
    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    /**
     * 查询当前套餐及其检查组和检查项信息
     * @param id
     * @return
     */
    @Override
    public Setmeal findAllById(int id) {
        return setmealDao.findAllById(id);
    }

    /**
     * 建立收藏和检查组的多对多关系
     */
    public void setSetmealAndCheckGroup(int setmealId, Integer[] checkGroupIds) {
        if (checkGroupIds != null && checkGroupIds.length > 0) {
            for (Integer checkGroupId : checkGroupIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("SetmealId", setmealId);
                map.put("CheckGroupId", checkGroupId);
                setmealDao.setSetmealAndCheckGroup(map);
            }
        }
    }

}
